package com.example.com.skills_utf8;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import anim.DIYAnimation;

import com.utils.Utils1;

public class AnimationActivity extends Activity{

	
	AnimationActivity activity = this;
	private Handler handler = new Handler();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animation);
		
		final EditText et_input1 =(EditText)findViewById(R.id.et_input1);
		
		Button bt_submit =(Button)findViewById(R.id.bt_submit);
		bt_submit.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		
				String str_content = et_input1.getText().toString();
				if( str_content != null){
					
					
					Animation shake =AnimationUtils.loadAnimation(activity, R.anim.shake);
					//et_input1.setAnimation(shake);
					if(Utils1.isContainNum(str_content)){
						et_input1.startAnimation(shake);
					}
					
					
					
				}
				
				
			}
		});
		
		
		// http://www.cnblogs.com/playing/archive/2011/08/21/2147874.html
		final Button btn_1 = (Button)findViewById(R.id.btn_1);
		final Button btn_2 = (Button)findViewById(R.id.btn_2);
		btn_1.setBackgroundResource(R.drawable.animation_refresh);
		
		final AnimationDrawable ad = (AnimationDrawable)btn_1.getBackground();
		btn_2.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			
				if(ad.isRunning() == false){
					ad.start();
					btn_2.setText("停止动画");
				}
				else
					ad.stop();
					btn_2.setText("启动动画");
				
			}
		});
		
		
	// 延迟执行
		final Button btn_3 = (Button)findViewById(R.id.btn_3);
		btn_3.setBackgroundResource(R.drawable.animation_refresh);
		handler.postDelayed(new Runnable() {
			public void run() {
			AnimationDrawable frameAnimation = (AnimationDrawable) btn_3.getBackground();
			frameAnimation.start();
			}
			}, 50);
		
		
		
	///////////////////////////////////////////////////////////////////////
		
		
		layout_collection = (LinearLayout)findViewById(R.id.layout_collection);
		params_collection = (LinearLayout.LayoutParams)layout_collection.getLayoutParams();
		params_collection.topMargin = 200;
		layout_collection.setLayoutParams(params_collection);
		
		
		cb_collection = (CheckBox)findViewById(R.id.cb_collection);
	//	Animation animation = AnimationUtils.loadAnimation(activity, R.anim.p);
	//	cb_collection.startAnimation(animation);
		
		cb_collection.setOnCheckedChangeListener( new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				
				
				//slideMenuInY(0, -200, params_collection.topMargin);
				
				float angle = (float)Math.toDegrees(30);
				float toXDelta = (float)Math.tan(angle) * 200;
				
				
				anima = new DIYAnimation(0, toXDelta, 0, -200);
				anima.setDuration(1500); 
				anima.setFillEnabled(false);
				// 设置动画监听器	
				
				cb_collection.startAnimation(anima);
			}
		});
		
		
		
		
		
		
		
	
	}
	
	
	
	DIYAnimation anima;
	TranslateAnimation slide;
	private CheckBox cb_collection;
	private LinearLayout.LayoutParams params_collection;
	private LinearLayout layout_collection;
	
	// 滑动函数
	public void slideMenuInX(int animateFromX, int animateToX, final int marginX)
	{
		slide = new TranslateAnimation(animateFromX, animateToX, 0, 0);
		slide.setDuration(300); // 每帧的时间  该方法是设置补间动画时间长度的主要方法，使用非常普遍。
		slide.setFillEnabled(true);
		
		// 设置动画监听器	
		slide.setAnimationListener(new AnimationListener()
		{
			public void onAnimationEnd(Animation animation) {		// Make movement of content permanent after animation has completed 
				params_collection.setMargins(marginX, 0, 0, 0);			// by positioning its left margin; margin是控件边缘相对父控件，或者其他控件的边距．
				cb_collection.setLayoutParams(params_collection); 
				System.out.println(""+ marginX); 						
			}

			public void onAnimationRepeat(Animation animation) { }
			public void onAnimationStart(Animation animation) { }
		});
		
		cb_collection.startAnimation(slide);		// Slide menu in or out
	}// end slideMenuIn
	
	
	
	
	public void slideMenuInY(int animateFromY, int animateToY, final int marginY)
	{
		slide = new TranslateAnimation(0, 0, animateFromY, animateToY);
		slide.setDuration(2000); // 每帧的时间  该方法是设置补间动画时间长度的主要方法，使用非常普遍。
		slide.setFillEnabled(false);
		
		// 设置动画监听器	
		slide.setAnimationListener(new AnimationListener()
		{
			// 动画的最后设置外边框
			public void onAnimationEnd(Animation animation) {		// Make movement of content permanent after animation has completed 
				params_collection.setMargins(0, marginY, 0, 0);			// by positioning its left margin; margin是控件边缘相对父控件，或者其他控件的边距．
				layout_collection.setLayoutParams(params_collection); 
				System.out.println(""+ marginY); 						
			}

			public void onAnimationRepeat(Animation animation) { }
			public void onAnimationStart(Animation animation) { }
		});
		
		cb_collection.startAnimation(slide);		// Slide menu in or out
	}// end slideMenuIn
	
	
	
}
